using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;

namespace M.Dao.Client.Core
{   
    
    public abstract class HttpServiceBase
    {

        protected string Get(string url, params KeyValuePair<string, string>[] headers)
        {   
            string result = UseHttpWebApproach(url, string.Empty, "GET", string.Empty,headers);

            return result;
        }

        protected string Post(string url, string body, params KeyValuePair<string, string>[] headers)
        {   
            string result = UseHttpWebApproach(url, string.Empty, "POST", body,headers);

            return result;
        }

        protected string Delete(string url, string body, params KeyValuePair<string, string>[] headers)
        {
            string result = UseHttpWebApproach(url, string.Empty, "DELETE", body,headers);

            return result;
        }

        protected string PostJsonObject(string url, object jsonObject, params KeyValuePair<string, string>[] headers)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            string json = serializer.Serialize(jsonObject);
            string result = UseHttpWebApproach(url, string.Empty, "POST", json,headers);

            return result;
        }
        private string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string requestBody, params KeyValuePair<string, string>[] headers)
        {
            string responseMessage = null;
            HttpWebRequest request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
            if (request != null)
            {
                //request.ContentType = "application/json";
                //request.ContentType = "application/x-www-form-urlencoded";
                request.Method = method;

                if (headers != null && headers.Length > 0)
                {
                    foreach (KeyValuePair<string, string> header in headers)
                    {
                        request.Headers.Add(header.Key, header.Value);
                    }
                }
            }

            if (method == "POST" && requestBody != null)
            {
                byte[] requestBodyBytes = ToByteArray(requestBody);

                request.ContentLength = requestBodyBytes.Length;

                using (Stream postStream = request.GetRequestStream())
                    postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
            }

            if (request != null)
            {
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
                        
                        responseMessage = reader.ReadToEnd();
                    }
                }
                else
                {
                    responseMessage = response.StatusDescription;
                }
            }
            return responseMessage;
        }

        private static byte[] ToByteArray(string requestBody)
        {
            byte[] bytes = null;
            bytes = Encoding.GetEncoding("utf-8").GetBytes(requestBody);

            return bytes;
        }
    }
}
